#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

#define Witch 720
#define Height 515

//定义用于储存图片的数组
IMAGE Img[2];
IMAGE sImg[12];
IMAGE bImg[12];

SYSTEMTIME startT;
MOUSEMSG m = { 0 };

//定义数组对应各图片所在位置 数组值为1-12
int SImg[12] = { 0 };
int BImg[4][3] = { 0 };
int num = 0;
int over;
int t;

void wdInit();
void loadAllImg();
void initGame();
void putImage();
void playGame();
void showTime();
int gameOver();

int main()
{
	wdInit();
	loadAllImg();
	initGame();
	GetSystemTime(&startT);
	BeginBatchDraw();
	do
	{
		
		playGame();
		putImage();
		showTime();
		gameOver();
		FlushBatchDraw();
	} while (gameOver() == 0);
	Sleep(300);
	EndBatchDraw();

	cleardevice();
	settextstyle(60, 0, L"宋体");
	settextcolor(RED);
	if (gameOver() == 1)
	{
		outtextxy(300, 100, L"WIN！");
	}
	else if (gameOver() == 2)
	{
		outtextxy(200, 100, L"LOSE！timeover");
	}

	
	getch();
	return 0;
}

//初始化窗口
void wdInit()
{
	//初始化窗口，边界线，分离区，图片区
	initgraph(Witch, Height);
	setbkcolor(WHITE);
	setcolor(BLACK);
	cleardevice();



	setfillcolor(RGB(240, 255, 255));
	fillrectangle(0, 405, 720, 420);

	setfillcolor(WHITE);

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			fillrectangle(i * 180, j * 135, i * 180 + 180, j * 135 + 135);
		}
	}

	for (int i = 0; i < 12; i++)
	{
		fillrectangle(i * 60, 420, i * 60 + 60, 465);
	}

	fillrectangle(0, 465, 720, 515);

	//游戏简要说明
	settextstyle(20, 0, L"宋体");
	settextcolor(BLACK);
	outtextxy(2, 470, L"简要游戏说明：分割线上方为贴图区，下方为选图区");
	outtextxy(2, 492, L"左键为选择图片，右键为贴上图片，总时间为 2分钟");

}

//初始化所有图片
void loadAllImg()
{
	

	//截取小图片
	loadimage(&Img[0], L"拼图图片.jpg", 240, 135);
	SetWorkingImage(&Img[0]);
	int m = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			getimage(&sImg[m++], i * 60, j * 45, 60, 45);
		}
	}
	SetWorkingImage(NULL);

	//截取大图片
	loadimage(&Img[1], L"拼图图片.jpg", Witch, 405);
	SetWorkingImage(&Img[1]);
	int n = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			getimage(&bImg[n++], i * 180, j * 135, 180, 135);
		}
	}
	SetWorkingImage(NULL);

}

//初始数据值
void initGame()
{
	
	for (int i = 0; i < 12; i++)
	{
		SImg[i] = i + 1;
	}
}

//调用图片
void putImage()
{
	//根据位置数组调用图片

	//贴图区域（大图片）
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (BImg[i][j]!=0)
			{
				putimage(i * 180, j * 135, &bImg[BImg[i][j]-1]);
			}
			else
			{
				fillrectangle(i * 180, j * 135, i * 180 + 180, j * 135 + 135);
			}
		}
	}

	//选图区域（小图片）
	for (int i = 0; i < 12; i++)
	{
		if (SImg[i] != 0)
		{
			putimage(i * 60, 420, &sImg[i]);
		}
		else
		{
			fillrectangle(i * 60, 420, i * 60 + 60, 465);
		}
	}
}

//鼠标操作
void playGame()
{
	m = GetMouseMsg();//接收当前鼠标信息
	
	//根据当前鼠标信息改变位置数组值
	switch (m.uMsg)
	{
		//左击 （选择图片）
		case WM_LBUTTONDOWN:
			if (m.y >= 420 && m.y <= 465)
			{
				int i;
				i = m.x / 60;
				if (SImg[i] != 0)
				{
					if (num != 0)
					{	
						SImg[num - 1] = num;	
					}
					num = SImg[i];
					SImg[i] = 0;
				}
			}
			if (m.y <= 405)
			{
				int i, j;
				i = m.x / 180;
				j = m.y / 135;
				if (BImg[i][j] != 0)
				{
					num = BImg[i][j];
					BImg[i][j] = 0;
				}
			}
			break;

		//右击 （贴上图片）
		case WM_RBUTTONDOWN:
			if (m.y <= 405)
			{

				int i,j;
				i = m.x / 180;
				j = m.y / 135;
				if (BImg[i][j]!=0)
				{
					SImg[BImg[i][j]-1] = BImg[i][j];
				}
				BImg[i][j] = num;
				num = 0;
			}
			break;
	}
}

//显示时间
void showTime()
{
	SYSTEMTIME T;
	GetSystemTime(&T);
	int t1, t2;
	int i, j;
	int a, b;
	

	t = 0;
	i = 0;
	j = 0;

	t += T.wHour - startT.wHour;
	t *=60;
	t += T.wMinute - startT.wMinute;
	t *= 60;
	t += T.wSecond - startT.wSecond;

	settextstyle(40, 0, L"宋体");
	outtextxy(610, 470, L":");

	t1 = t;
	t2 = t / 60;
	while (i != 2)
	{
		if (i == 0)
		{
			t1 = t % 60;
		}
		a = t1 % 10;
		TCHAR A[1];
		_stprintf(A, _T("%d"), a);
		outtextxy(675-i*40,470,A);
		t1 = t1 / 10;
		i++;
	}
	while (j != 2)
	{
		b = t2 % 10;
		TCHAR B[1];
		_stprintf(B, _T("%d"), b);
		outtextxy(590-j*40, 470, B);
		t2 = t2 / 10;
		j++;
	}



}

//判断游戏结束
int gameOver()
{
	int a = 0, b = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			a++;
			if (BImg[i][j] == a)
				b++;
		}
	}
	if (b == 12)
		return 1;
	else if (t == 120)
		return 2;
	else
	return 0;
}